﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{

    public class Projectile : RegularModelObject
    {
        public Vector3 InitialVelocity { get; set; }

        protected int lifeTimeMs { get; set; }
        protected int timeFromShotMs;

        protected ParticleSystem pSystem;
        protected float pSystemOffset;

        protected Ship shooter;

        protected RegularMovement regMove;

        public Projectile(Scene scene, Ship shooter, ModelUnit model, int layerIndex,
                          Vector3 position, Vector3 rotation)
            : base(scene, model, layerIndex)
        {
            this.position = position;
            this.rotation = rotation;
            
            this.shooter = shooter;
            timeFromShotMs = 0;

            Health = 1.0f;
            MaxHealth = Health;
            damage = 5.0f;
            lifeTimeMs = 100;

            pSystemOffset = 780.0f;

            World = Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateRotationX(rotation.X)
                * Matrix.CreateRotationY(rotation.Y);
            moveDir = Vector3.Transform(Vector3.Forward, World);
            World = World * Matrix.CreateTranslation(position);

            InitialVelocity = new Vector3();

            InitParticleSystem(scene);

            regMove = new RegularMovement();
            // no need for setting RegularMovement properties - only one method is used,
            // the one that doesn't require parameters other than ones passed directly
        }

        public Projectile(Scene scene, GameObject owner, ModelUnit model, int layerIndex, int lifeTimeMs)
            : base(scene, model, layerIndex)
        {
            this.lifeTimeMs = lifeTimeMs;
            timeFromShotMs = 0;

            Health = 1.0f;
            MaxHealth = Health;
            damage = 5.0f;

            InitParticleSystem(scene);
        }

        protected virtual void InitParticleSystem(Scene scene)
        {
            pSystem = null;
        }

        /// <summary>
        /// Standard update, that may be sufficient for basic projectiles.
        /// Feel free to override.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            ControlLifetime(gameTime);

            World = Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateRotationX(rotation.X)
                * Matrix.CreateRotationY(rotation.Y);
            moveDir = Vector3.Transform(Vector3.Forward, World);

            Vector3 initialVelocity = InitialVelocity;
            regMove.MoveWithAdditionalVelocity(ref position, moveSpeed, ref moveDir,
                ref initialVelocity, out position, out velocity);

            if (pSystem != null)
            {
                pSystem.Update(gameTime, position - moveDir * pSystemOffset,
                    World, velocity);
            }

            World = World * Matrix.CreateTranslation(position);

            bSphereTransformedUpToDate = false;
        }

        protected void ControlLifetime(GameTime gameTime)
        {
            timeFromShotMs += gameTime.ElapsedGameTime.Milliseconds;
            if (timeFromShotMs > lifeTimeMs)
            {
                Destroyed();
            }
        }

        public override void DrawTransparent(GameTime gameTime)
        {
            if (pSystem != null)
            {
                pSystem.Draw(gameTime);
            }
        }

        protected override void Explode()
        {
            scene.ExplosionMgr.SmallRedExplosion(position);
        }



        public override bool Collide(GameObject obj)
        {
            if (obj == shooter)
                return false;
            return true;
        }
        
    }
}
